package com.im.holder;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;

import com.core.sdk.ui.holder.BaseViewHolder;
import com.core.sdk.utils.ImageUtil;
import com.im.R;
import com.im.model.XmppMessage;

public class SmackMessageHolder extends BaseViewHolder<XmppMessage, Void> {

	TextView tvTitleLeft;
	TextView tvContentLeft;

	TextView tvTitleRight;
	TextView tvContentRight;

	@Override
	protected void onInitViews(View view) {
		tvTitleLeft = find(R.id.item_message_title_left);
		tvContentLeft = find(R.id.item_message_content_left);

		tvTitleRight = find(R.id.item_message_title_right);
		tvContentRight = find(R.id.item_message_content_right);
	}

	@Override
	protected void onBindItem() {
		XmppMessage sm = getItem().getData();
		if (sm.getMsgType() == XmppMessage.MSG_TYPE_TEXT)
			bindText(sm);
		else if (sm.getMsgType() == XmppMessage.MSG_TYPE_IMG) {
			String filePath = sm.getMsgContent();
			String fromUser = sm.getFromUser();
			bindImage(fromUser, filePath);
		} else if (sm.getMsgType() == XmppMessage.MSG_TYPE_AUDIO) {
			String filePath = sm.getMsgContent();
			String fromUser = sm.getFromUser();
			String desc = sm.getDesc();
			bindAudio(fromUser, filePath, desc);
		}
	}

	@Override
	protected void onResetViews() {

	}

	@Override
	protected void onRecycleItem() {

		recycleBitmap();
	}

	@Override
	protected void onRefreshView() {
		onBindItem();
	}

	@Override
	protected void onDestroy() {
		tvTitleLeft = null;
		tvContentLeft = null;
		tvTitleRight = null;
		tvContentRight = null;
	}

	private void bindText(XmppMessage sm) {
		String from = sm.getFromUser();
		try {
			if (getUserIdFromJID(from).equals(getUserIdFromJID(me))) { // 发出去的消息
				String tt = from.substring(0, from.indexOf("@"));
				tvTitleRight.setText(":" + tt);
				tvContentRight.setText(sm.getMsgContent());

				tvTitleLeft.setVisibility(View.GONE);
				tvContentLeft.setVisibility(View.GONE);
				
				tvTitleRight.setVisibility(View.VISIBLE);
				tvContentRight.setVisibility(View.VISIBLE);
			} else { // 收到的消息
				String tt = from.substring(0, from.indexOf("@"));
				tvTitleLeft.setText(tt + ":");
				tvContentLeft.setText(sm.getMsgContent());

				tvTitleLeft.setVisibility(View.VISIBLE);
				tvContentLeft.setVisibility(View.VISIBLE);
				
				tvTitleRight.setVisibility(View.GONE);
				tvContentRight.setVisibility(View.GONE);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private String getUserIdFromJID(String jid){
		return jid.substring(0,jid.lastIndexOf("@"));
	}

	Bitmap bmp = null;
	SpannableString spannableString = new SpannableString("test");
	ImageSpan imageSpan = null;

	private void bindImage(String fromUser, String filePath) {
		recycleBitmap();
		bmp = ImageUtil.getBitmap(filePath);
		try {
			if (getUserIdFromJID(fromUser).equals(getUserIdFromJID(me))) { // 发送出去的消息
				String tt = fromUser.substring(0, fromUser.indexOf("@"));
				tvTitleRight.setText(":" + tt);

				imageSpan = new ImageSpan(getRootView().getContext(), bmp);
				spannableString.setSpan(imageSpan, 0, spannableString.length(), SpannableString.SPAN_MARK_MARK);
				tvContentRight.setText("");
				tvContentRight.append(spannableString);

				tvTitleLeft.setVisibility(View.GONE);
				tvContentLeft.setVisibility(View.GONE);
				
				tvTitleRight.setVisibility(View.VISIBLE);
				tvContentRight.setVisibility(View.VISIBLE);
			} else { // 接收到的图片
				String tt = fromUser.substring(0, fromUser.indexOf("@"));
				tvTitleLeft.setText(tt + ":");
				imageSpan = new ImageSpan(getRootView().getContext(), bmp);
				spannableString.setSpan(imageSpan, 0, spannableString.length(), SpannableString.SPAN_MARK_MARK);
				tvContentLeft.setText("");
				tvContentLeft.append(spannableString);

				tvTitleLeft.setVisibility(View.VISIBLE);
				tvContentLeft.setVisibility(View.VISIBLE);
				
				tvTitleRight.setVisibility(View.GONE);
				tvContentRight.setVisibility(View.GONE);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void recycleBitmap() {
		if (bmp != null) {
			if (imageSpan != null) {
				spannableString.removeSpan(imageSpan);
				removeImageSpan(imageSpan, tvContentRight.getEditableText());
				removeImageSpan(imageSpan, tvContentLeft.getEditableText());
				imageSpan = null;
			}
			bmp.recycle();
			bmp = null;
		}
	}

	private void removeImageSpan(ImageSpan span, Editable editable) {
		if (editable == null)
			return;
		int start = editable.getSpanStart(span);
		int end = editable.getSpanEnd(span);
		if (start >= 0 && end > start) {
			editable.delete(start, end);
		}
	}

	private void bindAudio(String fromUser, String filePath, String desc) {
		try {
			if (getUserIdFromJID(fromUser).equals(getUserIdFromJID(me))) { // 发送出去的消息
				String tt = fromUser.substring(0, fromUser.indexOf("@"));
				tvTitleRight.setText(":" + tt);

				tvContentRight.setText(desc);
				Drawable drawable = getMyContext().getResources().getDrawable(R.drawable.icon_record_user_nor);
				// / 这一步必须要做,否则不会显示.
				drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
				tvContentRight.setCompoundDrawables(null, null, drawable, null);

				tvContentLeft.setVisibility(View.GONE);
				tvContentRight.setVisibility(View.VISIBLE);
			} else { // 接收到的图片
				String tt = fromUser.substring(0, fromUser.indexOf("@"));
				tvTitleLeft.setText(tt + ":");
				tvContentLeft.setText(desc);
				Drawable drawable = getMyContext().getResources().getDrawable(R.drawable.icon_record_friend_nor);
				// / 这一步必须要做,否则不会显示.
				drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
				tvContentLeft.setCompoundDrawables(null, null, drawable, null);

				tvContentLeft.setVisibility(View.VISIBLE);
				tvContentRight.setVisibility(View.GONE);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String me = null;
	public SmackMessageHolder(View rootView, String me, Context act) {
		super(rootView, act);
		this.me = me;
	}
}
